﻿
@{
    ViewData["Title"] = "NewsList";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section Scripts
    {
    <link href="~/css/page.css" rel="stylesheet" />
    <script type="text/html" id="barAction">
        <a class="layui-btn layui-btn-xs" lay-event="uploadPictures">上传图片</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
    <script type="text/html" id="IsRelease">
        {{# if(d.isImages==true){ }}
        {{# if (d.status === 1) { }}
        <a class="layui-btn layui-btn-xs" lay-event="noRelease">取消发布</a>
        {{# } else { }}
        <a class="layui-btn layui-btn-xs " lay-event="release">发布</a>
        {{# } } }}

    </script>
    <script type="text/javascript">
        var selfLayer = null, page = 1, active = null;
        layui.use(['layer', 'table', 'laydate'], function () {
            var layer = layui.layer //弹层
                ,
                table = layui.table, form = layui.form; //表格
            layui.laydate.render({
                elem: '#RecordTime',
                type: 'month',
                range: true
            })


            //执行一个 table 实例
            table.render({
                id: 'dataList',
                elem: '#dataview',
                height: 'full-80',
                url: "/NewsArticle/GetNewsArticleAll",
                parseData: function (res) { //res 即为原始返回的数据
                    console.log(res);
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": res.result.data //解析数据列表
                    };
                },
                response: {
                    statusCode: 200 //规定成功的状态码，默认：0
                },
                done: function (res, curr, count) {
                    page = curr;
                },
                page: true,
                cols: [
                    [
                        { field: 'title', title: '标题', width: '25%' },
                        { field: 'releaseTime', title: '发布时间', width: '10%' },
                        { field: 'type', title: '文章类型', width: '10%' },
                        { field: 'author', title: '发布人', width: '10%' },
                        { title: '是否发布', align: 'center', toolbar: '#IsRelease', width: '10%' },
                        { fixed: 'right', width: '35%', title: '操作', align: 'center', toolbar: '#barAction' }
                    ]
                ]
            });

            var $b = layui.$;
            active = {
                reload: function (page) {
                    //执行重载
                    table.reload('dataList', {
                        page: {
                            curr: page || 1 //重新从第 1 页开始
                        },
                        where: { keyword: $("#keyword").val() }

                    });
                }
            };
            $b('#search').on('click', function () {
                var type = $b(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            //监听工具条
            table.on('tool(datalist)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,
                    layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'uploadPictures') {
                    window.location.href = "/NewsArticle/ImportHeadDiagram?guid=" + data.id + "&type=新闻动态";
                } else if (layEvent === 'del') {
                    layer.confirm('数据删除不能恢复，您要继续？', function (index) {
                        layer.close(index);

                        axios.get('/NewsArticle/GetUpdateStatusById', {
                            params: {
                                Id: data.id, Type: "delete"
                            }
                        }).then(function (response) {
                            console.log(response);
                            if (response.status == 200) {
                                obj.del();
                                layer.msg("刪除成功", { icon: 1, time: 800, });
                                location.reload();
                            } else {
                                alert(response.msg)
                            }
                        }).catch(function (error) {
                            console.log(error);
                        });
                    });

                } else if (layEvent === 'edit') {
                    window.location.href = "/NewsArticle/Edit/" + data.id;
                }
                else if (layEvent === 'release') {
                    layer.confirm('确定发布？', function (index) {
                        layer.close(index);
                        axios.get('/NewsArticle/GetUpdateStatusById', {
                            params: {
                                Id: data.id, Type: "edit"
                            }
                        }).then(function (response) {
                            console.log(response);
                            if (response.status == 200) {
                                layer.msg("发布成功", { icon: 1, time: 800, });
                                location.reload();
                            } else {
                                alert(response.msg)
                            }
                        }).catch(function (error) {
                            console.log(error);
                        });
                    });
                }
                else if (layEvent === 'noRelease') {
                    layer.confirm('是否取消发布？', function (index) {
                        layer.close(index);
                        axios.get('/NewsArticle/GetUpdateStatusById', {
                            params: {
                                Id: data.id, Type: "edit"
                            }
                        }).then(function (response) {
                            console.log(response);
                            if (response.status == 200) {
                                layer.msg("取消成功", { icon: 1, time: 800, });
                                location.reload();
                            } else {
                                alert(response.msg)
                            }
                        }).catch(function (error) {
                            console.log(error);
                        });
                    });
                }
            });

        });
    </script>
}

<blockquote class="layui-elem-quote layui-text layui-form">
    <div class="layui-inline">
        <label class="layui-form-label">关键字：</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" id="keyword" name="keyword" style="width:240px" placeholder="">
        </div>
    </div>

    <div class="layui-inline">
        <div class="layui-form-label">
            <button class="layui-btn" id="search" style="width: 60px; height: 28px; line-height: 28px; display: inline" data-type="reload">搜索</button>
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-form-block">
            <a asp-action="Add" class="layui-btn layui-btn-primary backIframe" style="width: 105px; height: 28px; line-height: 28px;">新增</a>
        </div>
    </div>
</blockquote>
<table class="layui-hide" id="dataview" lay-filter="datalist"></table>

